#!/bin/bash
# 重启RocketMQ Broker脚本

echo "=========================================="
echo "重启RocketMQ Broker"
echo "=========================================="

# 停止Broker
echo "停止Broker..."
pkill -f "org.apache.rocketmq.broker.BrokerStartup" || echo "Broker未运行"

# 等待进程完全停止
sleep 3

# 检查是否还有Broker进程
if pgrep -f "org.apache.rocketmq.broker.BrokerStartup" > /dev/null; then
    echo "⚠️  Broker进程仍在运行，强制停止..."
    pkill -9 -f "org.apache.rocketmq.broker.BrokerStartup"
    sleep 2
fi

echo "✓ Broker已停止"

# 启动Broker
echo ""
echo "启动Broker..."
cd ~/tools/rocketmq/bin
export ROCKETMQ_HOME=~/tools/rocketmq
nohup sh mqbroker -n localhost:9876 > /dev/null 2>&1 &

# 等待启动
sleep 5

# 检查Broker是否启动成功
if lsof -i :10909 > /dev/null 2>&1; then
    echo "✅ Broker启动成功（端口10909）"
else
    echo "❌ Broker启动失败，请检查日志: ~/logs/rocketmqlogs/broker.log"
    exit 1
fi

echo ""
echo "验证Broker状态..."
cd ~/tools/rocketmq/bin
export ROCKETMQ_HOME=~/tools/rocketmq
sh mqadmin clusterList -n localhost:9876 2>&1 | grep -E "(Broker|ACTIVATED)" | head -3

echo ""
echo "=========================================="
echo "✅ Broker重启完成"
echo "=========================================="

